package com.fosafer.idcard_demo.act;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.fosafer.idcard_demo.R;
import com.fosafer.idcard_demo.util.IDCard;
import com.fosafer.idcard_demo.util.ToastUtil;
import com.fosafer.lib.FOSKeys;


/**
 * PackageName : [com.fosafer.idcard_demo.act]
 * ClassName   : [HomeAct]
 * Author      : [DoubleCui]
 * Description : []
 * CreateDate  : [2017/5/17]
 * UpdateDate  : [2017/5/17]
 * UpdateRemark: []
 * Since       : [1.0.0]
 * Copyright (C) 2017 Fosasfer Co.,Ltd.All rights reserved.
 */

public class UserInfoAct extends BaseAct implements View.OnClickListener {
    private ImageView act_user_info_iv_del_id;
    private ImageView act_user_info_iv_del_name;
    private EditText act_user_info_et_name;
    private EditText act_user_info_et_id;
    private String mName;
    private String mIDCaRd;


    TextWatcher nameTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() > 0) {
                act_user_info_iv_del_name.setVisibility(View.VISIBLE);
            } else {
                act_user_info_iv_del_name.setVisibility(View.GONE);

            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
    TextWatcher idTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() > 0) {
                act_user_info_iv_del_id.setVisibility(View.VISIBLE);
            } else {
                act_user_info_iv_del_id.setVisibility(View.GONE);

            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.act_user_info_btn_next:
                if (checkData()) {
                    skip(new String[]{FOSKeys.KEY_ID_HOLDER, FOSKeys.KEY_ID_CARD}, new String[]{mName, mIDCaRd}, FaceVerifyAct.class, false);
                }
                break;
            case R.id.act_user_info_iv_del_name:
                act_user_info_et_name.setText("");
                break;
            case R.id.act_user_info_iv_del_id:
                act_user_info_et_id.setText("");
                break;
            case R.id.layout_top_tv_left:
                finish();
                break;
        }
    }


    private boolean checkData() {
        mName = act_user_info_et_name.getText().toString();
        mIDCaRd = act_user_info_et_id.getText().toString();
        if (TextUtils.isEmpty(mName)) {
            ToastUtil.showShort(R.string.tips_name_error);
            return false;
        }
        if (TextUtils.isEmpty(mIDCaRd) || !new IDCard().verify(mIDCaRd)) {
            ToastUtil.showShort(R.string.tips_id_error);
            return false;
        }
        /*if (TextUtils.isEmpty(mIDCaRd)) {
            ToastUtil.showShort(R.string.tips_id_error);
            return false;
        }*/
        return true;
    }

    @Override
    protected void initView() {
        super.initView();
        Button act_user_info_btn_next = (Button) findViewById(R.id.act_user_info_btn_next);
        TextView layout_top_tv_title = (TextView) findViewById(R.id.layout_top_tv_title);
        act_user_info_iv_del_id = (ImageView) findViewById(R.id.act_user_info_iv_del_id);
        act_user_info_iv_del_name = (ImageView) findViewById(R.id.act_user_info_iv_del_name);
        act_user_info_et_name = (EditText) findViewById(R.id.act_user_info_et_name);
        act_user_info_et_id = (EditText) findViewById(R.id.act_user_info_et_id);

        act_user_info_et_name.addTextChangedListener(nameTextWatcher);
        act_user_info_et_id.addTextChangedListener(idTextWatcher);
        act_user_info_btn_next.setOnClickListener(this);
        act_user_info_iv_del_name.setOnClickListener(this);
        act_user_info_iv_del_id.setOnClickListener(this);

        layout_top_tv_title.setText("请输入姓名和身份证");


    }

    @Override
    public int getContentViewLayoutId() {
        return R.layout.act_user_info;
    }
}
